from flask import Flask, render_template,url_for,request

app = Flask(__name__, static_url_path='/static', static_folder='static', template_folder='templates')
app.config['SECRET_KEY'] = os.urandom()
# 主页地址
@app.route('/')
def index():
    return render_template('index.html')

# 文章内容
@app.route('/article')
def read():
    return render_template('article-user.html')
# 用户注册
@app.route('/user/reg',methods=['POST'])
def register():
    return render_template('post-user.html')

# 查询参数
@app.route('/test')
def test():
    username = request.args.get('username')
    password = request.args.get('password')
    return '用户名为：%s , 密码为：%s' %(username,password)

@app.route('/article/<int:articleid>-<int:page>')
def article(articleid, page):
    return f"正在访问编号为;{articleid}的文章"


@app.route('/user/login', methods=['POST'])
def login():
    username = request.form.get('username')
    password = request.form.get('password')
    if username == 'cgm' and password == '123456':
        return 'success'
    else:
        return 'fail'
# session 保存在服务器端，可直接返回参数
@app.route('/session')
def session():
    session['islogin']=True
    session['username']='cgm'
    session['passsword']='123'
    session['role'] = 'editor'
    return 'the session name is %s'%session.get('username')
# cookie浏览器自己设置
@app.route('/cookie')
def cookie():
    response = make_response()
    response.set_cookie('username','cgm',max_age=30)
    response.set_cookie('password','123',max_age=30)
    return response

@app.route('/sc/read')
def scread():
    return 'local name is %s'%request.cookies.get('username')

if __name__ == '__main__':

    app.run(debug=True)
